package com.zhuimeng.Application.entity;

import java.util.ArrayList;
import java.util.List;

public class GoBank {
    private Integer[][] gobang = new Integer[17][17];
    private List<Integer[]> bangStep = new ArrayList<>();

    public boolean go(int x, int y, int color) {
        if(x>=17||y>=17){
            return false;
        }
        Integer z = gobang[x][y];
        if (z == null || z == 2) {
            gobang[x][y] = color;
            bangStep.add(new Integer[]{x, y, color});
            System.out.println(bangStep.get(bangStep.size()-1)[0]+","+bangStep.get(bangStep.size()-1)[1]+","+bangStep.get(bangStep.size()-1)[2]);
            return true;
        }
        return false;
    }

    public boolean isWin(int x, int y, int color) {

        int clientX = x;
        int clientY = y;
        int count = 1;
        while (x > 0 && gobang[--x][y] != null && gobang[x][y] == color) {
            count++;
        }
        x = clientX;
        y = clientY;
        while (x < gobang.length - 1 && gobang[++x][y] != null && gobang[x][y] == color) {
            count++;
        }
        if (count >= 5) {
            return true;
        }
        count = 1;
        x = clientX;
        y = clientY;
        while (y > 0 && gobang[x][--y] != null && gobang[x][y] == color) {
            count++;
        }
        x = clientX;
        y = clientY;
        while (y < gobang.length - 1 && gobang[x][++y] != null && gobang[x][y] == color) {
            count++;
        }
        if (count >= 5) {
            return true;
        }
        count = 1;
        x = clientX;
        y = clientY;
        while (y > 0 && x > 0 && gobang[--x][--y] != null && gobang[x][y] == color) {
            count++;
        }
        x = clientX;
        y = clientY;
        while (y < gobang.length - 1 && x < gobang.length - 1 && gobang[++x][++y] != null && gobang[x][y] == color) {
            count++;
        }
        if (count >= 5) {
            return true;
        }
        count = 1;
        x = clientX;
        y = clientY;
        while (y > 0 && x < gobang.length - 1 && gobang[++x][--y] != null && gobang[x][y] == color) {
            count++;
        }
        x = clientX;
        y = clientY;
        while (y < gobang.length - 1 && x > 0 && gobang[--x][++y] != null && gobang[x][y] == color) {
            count++;
        }
        if (count >= 5) {
            return true;
        }
        return false;
    }

    public List getStep(int num) {
        System.out.println("bangStep:"+bangStep);
        for (int i = 1; i <= num; i++) {
            if(bangStep.size() - 1!=-1){
                gobang[bangStep.get(bangStep.size() - 1)[0]][bangStep.get(bangStep.size() - 1)[1]] = 2;
                bangStep.remove(bangStep.size() - 1);
            }
        }
        return bangStep;
    }

    public int clearBand() {
        for (int i = 0; i < gobang.length; i++) {
            for (int j = 0; j < gobang.length; j++) {
                gobang[i][j] = 2;
            }
        }
        bangStep.clear();
        return 1;
    }
}
